<template>
    <div ref="chartBarRef" class="chart-box bg-white p-16px">
        <div class="chart-box">
            <div class="flex justify-between">
                <div class="text-14px bold">{{ title }}</div>
                <div class="flex text-12px">
                    <customer-switch v-model:active="active" class="ml-20px" :list="list" @change="handleChange"></customer-switch>
                </div>
            </div>
            <div class="chart-container" ref="chartRef">
            </div>
        </div>
    </div>
</template>
<script setup>
import { ref, watch, computed, onMounted, onActivated } from 'vue'
import * as echarts from 'echarts'
import { getEleSignNearlySeven } from '@/api/clients/signClient.ts'
import { getEleSignTrend } from '@/api/dashboard/eleSign.ts'

const props = defineProps({
    title: {
        type: String,
        default: '近七日签署趋势'
    },
    showLegend: {
        type: Boolean,
        default: false
    },
    companyId: {
        type: [String, Number]
    },
    insureType: {
        type: String,
        default: 'eleSign'
    },
    queryData: {
        type: Object,
        default() {
            return {}
        }
    }
})

const active = ref(1)
const list = ref([
    {
        name: '签署份数',
        value: 1
    },
    {
        name: '待签署',
        value: 2
    },
    {
        name: '签署失败',
        value: 3
    },
])

const handleChange = (val) => {
    console.log(444, active.value)
    setOption()
}

const chartRef = ref(null)
const chartBarRef = ref(null)
let myChart = null
const chartData = ref({})

const chartTitle = () => {
    const cur = list.value.find(i => i.value === active.value)
    return cur.name
}

const createChartOption = (date, data) => {
    const chartOption =
    {
        tooltip: {
        },
        xAxis: {
            type: 'category',
            data: date,
            axisLine: {
                show: true,
                lineStyle: {
                    color: '#EAEAEF',
                    type: 'solid'
                },
            },
            axisLabel: {
                show: true,
                color: '#9497B1',
                margin: 20
            }
        },
        yAxis: {
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            },
            axisLabel: {
                show: true,
                color: '#9497B1'
            }
        },
        grid: {
            top: 40,
            bottom: 60,
            left: 40,
            right: 40,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow',
                }
            }
        },
        series: [{
            name: chartTitle(),
            type: 'bar',
            data: data,
            seriesLayoutBy: 'row',
            barWidth: '20px',
            color:
                new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                        offset: 0,
                        color: '#1A64FD'
                    },
                    {
                        offset: 1,
                        color: '#657EFB'
                    }
                ])
        }]
    }
    if (props.showLegend) {
        chartOption.legend = {
            right: 0,
            top: 10
        }
    }
    return chartOption
}


const setOption = () => {
    const map = {
        1: 'signQuantity',
        2: 'toBeSigned',
        3: 'signingFailed'
    }
    myChart.setOption(createChartOption(chartData.value.day, chartData.value[map[active.value]]))
}

const getData = () => {
    if(props.insureType === 'eleSign') {
        getEleSignNearlySeven({
            companyId: props.companyId,
            ...props.queryData
        }).then(res => {
            chartData.value = res
            setOption()
        })
    } else if (props.insureType === 'dashBoardEleSign') {
        getEleSignTrend({
            ...props.queryData
        }).then(res => {
            chartData.value = res
            setOption()
        })
    }
}

const resize = () => {
    myChart.resize()
}

defineExpose({
    chartBarRef,
    resize
})

watch(() => props, (newval) => {
    console.log(props)
    if (newval.insureType === 'eleSign') {
        if(newval.companyId) {
            getData()
        }
    } else {
        getData()
    }
}, {
    immediate: true,
    deep: true
})

onMounted(() => {
    myChart = echarts.init(chartRef.value)
})
onActivated(() => {
    myChart = echarts.init(chartRef.value)
})
</script>
<style lang="scss" scoped>
.chart-box {
    box-sizing: border-box;
}

.chart-box,
.chart-container {
    width: 100%;
    height: 100%;
}

.icon {
    width: 12px;
    height: 12px;
    border-radius: 2px;
}

.blue {
    background: #2567FC;
}

.light-blue {
    background: #EBEFFE;
}
</style>